package id.co.shuba.spamobile.dialog;

import id.co.shuba.spamobile.util.DialogUtil;

import java.util.Calendar;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.text.format.Time;
import android.view.ContextThemeWrapper;
import android.widget.DatePicker;
import android.widget.EditText;

public class DatePickerFragment extends DialogFragment implements
		DatePickerDialog.OnDateSetListener {

	private EditText _txt;
	
	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {		
		final Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
					
		String date = _txt.getText().toString();
		
		if (!"".equals(date)) {
			try {
				String[] values = date.split("-");
				year = Integer.parseInt(values[2]);
				month = Integer.parseInt(values[1]) -1;
				day = Integer.parseInt(values[0]);
			} catch (Exception e) {					
			}				
		}
		
		return new DatePickerDialog(new ContextThemeWrapper(getActivity(),
				id.co.shuba.spamobile.R.style.Theme_Sherlock_Light_Dialog), this, year, month, day);
	}

	public void onDateSet(DatePicker view, int year, int month, int day) {
		Time chosenDate = new Time();
		chosenDate.set(day, month, year);
		long dtDob = chosenDate.toMillis(true);
		CharSequence strDate = DateFormat.format(
				"dd-MM-yyyy", dtDob);
		_txt.setText(strDate);
	}
	
	public void setTarget(EditText txt) {
		_txt = txt;
	}
}
